package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmSensitivePageDto;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.WmSensitiveService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@RequiredArgsConstructor
@Slf4j
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements WmSensitiveService {


    private final WmSensitiveMapper wmSensitiveMapper;

    /**
     * 分页查看敏感词
     *
     * @param wmSensitivePageDto
     * @return
     */
    @Override
    public ResponseResult listSensitive(WmSensitivePageDto wmSensitivePageDto) {

        //1.检查参数
        if (wmSensitivePageDto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "敏感词分页参数不存在");
        }

        //2.分页
        IPage page = new Page(wmSensitivePageDto.getPage(), wmSensitivePageDto.getSize());
        LambdaQueryWrapper<WmSensitive> lambdaqueryWrapper = new LambdaQueryWrapper<>();

        //3.按照创建时间倒叙查询
        lambdaqueryWrapper.orderByDesc(WmSensitive::getCreatedTime);


        //4.按照敏感词名称模糊查询
        if (wmSensitivePageDto.getName() != null) {
            lambdaqueryWrapper.like(WmSensitive::getSensitives, wmSensitivePageDto.getName());
        }

        //5.分页查询
        page = page(page, lambdaqueryWrapper);

        //3.结果返回
        ResponseResult responseResult = new PageResponseResult(wmSensitivePageDto.getPage(), wmSensitivePageDto.getSize(), (int) page.getTotal());
        responseResult.setData(page.getRecords());

        return responseResult;
    }


    /**
     * 新增敏感词
     *
     * @param wmSensitive
     * @return
     */
    @Override
    public ResponseResult saveSensitive(WmSensitive wmSensitive) {

        //1.检查参数
        if (wmSensitive == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "新增敏感词参数不存在");
        }

        //2.查看改敏感词是否存在

        List<WmSensitive> wmSensitives = wmSensitiveMapper.selectList(Wrappers.<WmSensitive>lambdaQuery().eq(WmSensitive::getSensitives, wmSensitive.getSensitives()));

        if (wmSensitives.size() > 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "敏感词已存在");
        }

        wmSensitive.setCreatedTime(new Date());

        //3.新增敏感词
        int insert = wmSensitiveMapper.insert(wmSensitive);

        if (insert <= 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR, "新增敏感词失败");
        }


        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


    /**
     * 修改敏感词
     *
     * @param wmSensitive
     * @return
     */
    @Override
    public ResponseResult updeteSensitive(WmSensitive wmSensitive) {

        //1.参数判断
        if (wmSensitive == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "修改敏感词参数无效");
        }


        //2.修改后的敏感词是否存在
        List<WmSensitive> wmSensitives = wmSensitiveMapper.selectList(Wrappers.<WmSensitive>lambdaQuery().eq(WmSensitive::getSensitives, wmSensitive.getSensitives()));

        if (wmSensitives.size() > 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "敏感词已存在");
        }

        wmSensitive.setCreatedTime(new Date());

        //3.修改
        int update = wmSensitiveMapper.updateById(wmSensitive);
        if (update <= 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR, "修改敏感词失败");
        }

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


    /**
     * 删除敏感词
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteSensitive(Integer id) {

        //1.参数判断
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "删除敏感词参数无效");
        }


        //2.删除
        int delete = wmSensitiveMapper.deleteById(id);
        if (delete <= 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR, "删除敏感词失败");
        }

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}













